/*******************************************************************************
Name: res_gradient
*******************************************************************************/


clear

capture log close

log using  "${logdir}6-res_gradient.log", replace


/*------------------------------------------------------------*/
*IMPORT AND SAVE TRACT-LEVEL CENSUS DATA
/*------------------------------------------------------------*/

*1970
insheet using "${rawdir}LTDB_Std_All_fullcount/LTDB_Std_1970_fullcount.csv"

keep trtid10 state county tract placefp10 cbsa10 metdiv10 ccflag10 pop70 white70 black70

destring pop70, replace
destring white70, replace
destring black70, replace

save "${datadir}trt70.dta", replace

clear

*1980
insheet using "${rawdir}LTDB_Std_All_fullcount/LTDB_Std_1980_fullcount.csv"

keep trtid10 state county tract placefp10 cbsa10 metdiv10 ccflag10 pop80 nhwht80 nhblk80 

rename nhwht80 white80
rename nhblk80 black80

save "${datadir}trt80.dta", replace

clear

*1990
insheet using "${rawdir}LTDB_Std_All_fullcount/LTDB_Std_1990_fullcount.csv"

keep trtid10 state county tract placefp10 cbsa10 metdiv10 ccflag10 pop90 nhwht90 nhblk90 

rename nhwht90 white90
rename nhblk90 black90

save "${datadir}trt90.dta", replace

clear

*2000
insheet using "${rawdir}LTDB_Std_All_fullcount/LTDB_Std_2000_fullcount.csv"

keep trtid10 state county tract placefp10 cbsa10 metdiv10 ccflag10 pop00 nhwht00 nhblk00 

rename nhwht00 white00
rename nhblk00 black00

save "${datadir}trt00.dta", replace

*combine years

merge trtid10 using "${datadir}trt70.dta", sort keep(pop70 white70 black70)

drop _merge

merge trtid10 using "${datadir}trt80.dta", sort keep(pop80 white80 black80)
drop _merge

merge trtid10 using "${datadir}trt90.dta", sort keep(pop90 white90 black90)
drop _merge

rm "${datadir}trt70.dta"
rm "${datadir}trt80.dta"
rm "${datadir}trt90.dta"
rm "${datadir}trt00.dta"

save "${datadir}tract_demo.dta", replace

clear

*import census tract locations

insheet using "${rawdir}Gaz_tracts_national.txt"

rename geoid trtid10
rename intptlat lat
rename intptlong lon
rename pop10 pop10_2
keep trtid10 pop10_2 lat lon

save "${datadir}tract_gps.dta", replace

merge trtid10 using "${datadir}tract_demo.dta", sort

tab _merge
assert _merge != 2
keep if _merge == 3
drop _merge

gen FIP5 = floor(trtid10/1000000)

*map counties to msafips
merge FIP5 using "${rawdir}80msa_necma_cw.dta", sort uniqusing keep(msafips msa_name necma_name FIP5)

keep if _merge == 3
drop _merge

*map msafips to metarea
merge msafips using "${rawdir}msafips_metarea.dta", sort uniqusing
keep if _merge == 3
drop _merge

*restrict to sample of MSAs
merge metarea using "${datadir}matched_msas.dta", sort uniqusing
keep if _merge == 3
drop _merge

*merge in MSA CBD coordinates
merge metarea using "${rawdir}cbd_coordinates.dta", sort uniqusing

drop if _merge == 2

egen keep = max(_merge == 3), by(metarea)
keep if keep == 1
drop keep _merge

keep if cbd_lat != . & cbd_lon != .

vincenty lat lon cbd_lat cbd_lon, vin(distv) hav(disth)

egen black70_msa = sum(black70), by(metarea)
egen black80_msa = sum(black80), by(metarea)
egen black90_msa = sum(black90), by(metarea)
egen black00_msa = sum(black00), by(metarea)

egen pop70_msa = sum(pop70), by(metarea)
egen pop80_msa = sum(pop80), by(metarea)
egen pop90_msa = sum(pop90), by(metarea)
egen pop00_msa = sum(pop00), by(metarea)

gen fblack70 = black70/pop70
gen fblack80 = black80/pop80
gen fblack90 = black90/pop90
gen fblack00 = black00/pop00

gen fblack70_n = fblack70/(black70_msa/pop70_msa)
gen fblack80_n = fblack80/(black80_msa/pop80_msa)
gen fblack90_n = fblack90/(black90_msa/pop90_msa)
gen fblack00_n = fblack00/(black00_msa/pop00_msa)

egen sample = max((pop70 == .)), by(trtid10)
replace sample = sample + 1
replace sample = 0 if sample == 2

areg fblack70_n disth [aw = pop70] if sample, absorb(metarea)
areg fblack80_n disth [aw = pop80] if sample, absorb(metarea)
areg fblack90_n disth [aw = pop90] if sample, absorb(metarea)
areg fblack00_n disth [aw = pop00] if sample, absorb(metarea)


areg fblack70_n disth [aw = pop70], absorb(metarea)
areg fblack80_n disth [aw = pop80], absorb(metarea)
areg fblack90_n disth [aw = pop90], absorb(metarea)
areg fblack00_n disth [aw = pop00], absorb(metarea)


/*------------------------------------------------------------*/
/*------------------------------------------------------------*/
* FIGURE 1(B): DISTANCE SLOPE FOR EARLY AND LATE PERIODS
/*------------------------------------------------------------*/
/*------------------------------------------------------------*/

capture rm ${datadir}res70.dta
capture rm ${datadir}res00.dta

*non-parametric plot

reg fblack70_n disth [aw = pop70] if disth < 40 & sample == 1, robust
local slope70 "1970 Slope = `:di %6.3f _b[disth]'"
local slope70_se "(`: di %4.3f _se[disth]')"

binsreg fblack70_n disth [aw = pop70] if disth < 40 & sample == 1, ///
	line(1 1) cb(1 1) nbins(35) vce(robust) savedata(${datadir}res70.dta)



reg fblack00_n disth [aw = pop00] if disth < 40 & sample == 1, robust
local slope00 "2000 Slope = `:di %6.3f _b[disth]'"
local slope00_se "(`: di %4.3f _se[disth]')"

binsreg fblack00_n disth [aw = pop00] if disth < 40 & sample == 1, ///
	line(1 1) cb(1 1) nbins(35) vce(robust) savedata(${datadir}res00.dta)


use ${datadir}res70.dta, clear
gen year = 1970

append using ${datadir}res00.dta
replace year = 2000 if year != 1970

keep line_x line_fit CB_l CB_r year

twoway (lpoly CB_r line_x if year == 1970, deg(1) kernel(gaussian) lcolor(gs4) lpattern(dot)) ///
	(lpoly CB_l line_x if year == 1970, deg(1) kernel(gaussian) lcolor(gs4) lpattern(dot)) ///
	(lpoly line_fit line_x if year == 1970, deg(1) kernel(gaussian) lcolor(gs4) lpattern(solid)) ///
	(lpoly CB_r line_x if year == 2000, deg(1) kernel(gaussian) lcolor(gs10) lpattern(dot)) ///
	(lpoly CB_l line_x if year == 2000, deg(1) kernel(gaussian) lcolor(gs10) lpattern(dot)) ///
	(lpoly line_fit line_x if year == 2000, deg(1) kernel(gaussian) lcolor(gs10) lpattern(dash)), ///
	legend(order(3 "1970" 6 "2000")) xtitle("Distance from CBD (Miles)") ///
	ytitle("Normalized Black Share of Residents") ///
	plotregion(fcolor(white)) graphregion(fcolor(white)) ///
	text(2.75 25 "`slope70'", place(e)  size(medium)) ///
	text(2.6 33 "`slope70_se'", place(e)  size(medium)) ///
	text(1.75 25 "`slope00'", place(e)  size(medium)) ///
	text(1.6 33 "`slope00_se'", place(e)  size(medium))
	
graph save "${outdir}distslope_res.gph", replace
graph export "${outdir}distslope_res.pdf", replace
	
rm ${datadir}res70.dta
rm ${datadir}res00.dta

log close
